'use strict'

define(['jquery', 'bootstrap', 'bootstrapTable', 'bootstrapTableCN', 'datetimepicker', 'datetimepickerCN', 'select2', 'validate', 'validate.en', 'shiftform',], function($) {
  
  return {
        
    init: function(data) {
        this.initPage(data.status);
        this.create();
        this.arrange();
    },
    
    initPage: function(status){
        
        $('select[name="site"]').select2({
            minimumResultsForSearch : -1,
            allowClear : true,
            width: '150'
          });

          $('select[name="site"]').change(function(){ 

            var site = $(this).children('option:selected').val();
            window.location.href = window.__CTX__.root + "/group-rounds?site=" + site + "&status=" + status;

          })
    },
    
    create: function() {
        $(document).on('shown.bs.modal', '#group-round-create', function(e){
            
            $('#executor-table').bootstrapTable({
                  height: 280,
                  striped: true
                })
                
                var course = $('select[name="course"]').select2({
                    placeholder : '请选择课程类型',
                    minimumResultsForSearch : 5,
                    allowClear : false,
                  }).on('change', function (e) {
                      
                      var courseId = this.value
                      
                      var $form = $("#search-form");
                      var parameters = $form.serializeArray()
                      
                      $.ajax({
                          type: $form.attr('method'),
                          url: $form.attr('action'),
                          data: parameters,
                          cache:false
                        }).done(function(data) {
                            
                            $('input[name="courseId"]').val(courseId)
                            
                            data = JSON.parse(data)
                            
                            if(data.code == 0) {
                                var rounds = data.data.courseRounds;
                                $('#executor-table').bootstrapTable("removeAll");
                                $('#executor-table').bootstrapTable("append", rounds);
                            }else {
                                $('#executor-table').parents('.bootstrap-table').show_err_tooltip('查询学员出错')
                            }
                        }).fail(function() {
                            $('#executor-table').parents('.bootstrap-table').show_err_tooltip('查询学员出错')
                        })
                    })
                    
                $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
            
                var $form = $(e.currentTarget).find(".bind-form")                          //bind shiftform
                $form.shiftform({
                  beforeSubmit : function(el) {
                      var data = $('#executor-table').bootstrapTable('getSelections')
                      
                      if(data.length > 0) {
                        var roundIds = []
                        for(var i in data) {
                          roundIds.push(data[i].id)
                        }
                        $('input[name="roundIds"]').val(roundIds)
                      }
                      
                      $('.bind-form').trigger("validate.ajax.submit")
                      var isSiteSelected = $('input[name="roundIds"]').val() == '' ? false : true;
                      if (!isSiteSelected) {
                        $('#executor-table').parents('.bootstrap-table').show_err_tooltip('请选择学员');
                        return false;
                      }
                    return $.bt_validate.result[$.bt_validate.form_id]
                  }
                })

        })
      },
      
      arrange: function() {
          $(document).on('shown.bs.modal', '#group-round-arrange', function(e){
              
                  $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
              
                  var $form = $(e.currentTarget).find(".form-horizontal")                          //bind shiftform
                  $form.shiftform({
                    beforeSubmit : function(el) {
                        var days = [];
                        $("select[name='day']").each(function() {
                            days.push($(this).val());
                        });
                        var hours =[]
                        $("select[name='hours']").each(function() {
                            hours.push($(this).val());
                      });
                        
                        var dayhour = []
                        
                        for(var i in days) {
                            dayhour.push(days[i] + "|" + hours[i])
                        }
                        
                        $("input[name='dayhour']").val(dayhour)
                      
                      $('.form-horizontal').trigger("validate.ajax.submit")
                      return $.bt_validate.result[$.bt_validate.form_id]
                    }
                  })

          })
        }
      
  }
})
